<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item>
        <el-button type="primary" size="mini" @click="getAdd"
          >新建报价</el-button
        >
      </el-form-item>
      <el-dialog :title="title" :visible.sync="dialogVisibleOne" width="30%">
        <el-form
          ref="addEditform"
          :rules="addEditformRules"
          :model="addEditform"
          label-width="100px"
        >
          <el-form-item label="报价类型：" prop="type">
            <el-select
              v-model="addEditform.type"
              placeholder="请选择"
              @change="changeType"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="报价名称：" prop="quoteTitle">
            <el-input v-model="addEditform.quoteTitle"></el-input>
          </el-form-item>
          <!-- <el-form-item label="报价基数" prop="scale">
            <el-input v-model="addEditform.scale"></el-input>
          </el-form-item> -->
          <el-form-item label="备注：" prop="remakk">
            <el-input v-model="addEditform.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleOne = false">取 消</el-button>
          <el-button type="primary" @click="addEditBtn">创建</el-button>
        </div>
      </el-dialog>
      <el-form-item prop="status">
        <el-input
          placeholder="请输入搜索关键字"
          v-model="page.quoteTitle"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表单 -->
    <el-table
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="序号" type="index" />
      <el-table-column
        width="150"
        align="center"
        prop="createTime"
        label="时间"
      >
        <template slot-scope="scope">
          <span>
            {{
              scope.row.createTime.slice(0, 10) +
              " " +
              scope.row.createTime.slice(11, 19)
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        align="center"
        prop="quotoTitle"
        label="标题"
      >
      </el-table-column>
      <el-table-column align="center" width="150" label="报价类型" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">展厅预算</span>
          <span v-else-if="scope.row.type == 2">大屏预算</span>
          <span v-else-if="scope.row.type == 3">弱点项目预算</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        width="150"
        label="总造价"
        prop="countCost"
      >
        <template slot-scope="scope">
          {{ scope.row.countCost.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        width="150"
        label="总成本"
        prop="countPrime"
      >
        <template slot-scope="scope">
          {{ scope.row.countPrime.toFixed(2) }}
        </template>
      </el-table-column>
        <el-table-column
        width="150"
        align="center"
        prop="profit"
        label="利润率"
      >
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" @click="editBtn(scope.row)">
            编辑</el-button
          >
          <el-button
            icon="el-icon-delete-solid"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button icon="el-icon-view" @click="hadprive(scope.row)"
            >预览</el-button
          >
          <el-button icon="el-icon-top" @click="expor(scope.row, 1)"
            >市场报价</el-button
          >
          <el-button icon="el-icon-top" @click="expor(scope.row, 2)"
            >成本清单</el-button
          >
          <el-button icon="el-icon-top" @click="expor(scope.row, 3)"
            >采购清单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 下载弹窗 -->
     <el-dialog :title="title" :visible.sync="downDialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
        class="formStyle"
      > 
        <el-form-item label="人员logo" prop="logoId">
           <el-select v-model="ruleForm.logoId" placeholder="请选择人员logo">
              <el-option
                v-for="item in logoIdList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>   
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="downDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="downEnter">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 预览 -->
    <el-dialog title="报价预览" :visible.sync="dialogVisibleTwo" width="80%">
      <div style="height: 50px; textalign: center">
        <h3>{{ quotoTitle }}</h3>
      </div>
      <div style="width: 80%; marginleft: 10%">
        <table>
          <tr>
            <td>区域</td>
            <td>展项</td>
            <td>产品名称</td>
            <!-- <td>产品尺寸</td> -->
            <td>产品型号</td>
            <!-- <td>产品单价</td> -->
            <td>产品数量</td>
            <td>产品参数</td>
            <td>单位</td>
            <th>未税单价</th>
            <th>未税合计</th>
            <th>税金</th>
            <th>未税成本</th>
            <th>未税成本总价</th>
          </tr>
          <tr v-for="item in list" :key="item.id">
            <td :rowspan="item.areaNamect" v-if="item.areaNameshow">
              {{ item.areaName }}
            </td>
            <td :rowspan="item.displayNamect" v-if="item.displayNameshow">
              {{ item.displayName }}
            </td>
            <td>{{ item.productName }}</td>
            <td>{{ item.model }}</td>
            <td>{{ item.productCount }}</td>
            <td>
              <el-popover
                popper-class="bgc"
                placement="top-start"
                trigger="hover"
                :content="item.param"
              >
                <div style="height: 150px" class="overCell" slot="reference">
                  {{ item.param }}
                </div>
              </el-popover>
            </td>
            <td>{{ item.unit }}</td>
            <td>{{ item.userPrice }}</td>
            <td>{{ item.userPriceCount }}</td>
            <td>{{ item.taxes }}</td>
            <td>{{ item.customerPrice }}</td>
            <td>{{ item.customerPriceCount }}</td>
          </tr>
          <tr>
            <td :colspan="7">合计</td>
            <td :colspan="3">{{ TotalPriceList.noTexSum }}</td>

            <td></td>
            <td>{{ TotalPriceList.costSum }}</td>
          </tr>
          <tr>
            <td :colspan="7">税金(13%)</td>
            <td :colspan="3">{{ TotalPriceList.noTexCount }}</td>

            <td></td>
            <td>{{ TotalPriceList.costTexCount }}</td>
          </tr>
          <tr>
            <td :colspan="7">含税总计</td>
            <td :colspan="3">{{ TotalPriceList.texSum }}</td>

            <td></td>
            <td>{{ TotalPriceList.costCount }}</td>
          </tr>
        </table>
      </div>
      <div class="footer">
        <!-- <div class="title">合计:{{ totalPrice }}元</div> -->
        <el-button
          style="margin: 0px 100px"
          type="primary"
          size="mini"
          plain
          @click="goHome"
          >退出</el-button
        >
      </div>
    </el-dialog>
    <!-- table -->

    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[1, 2, 3, 4, 5, 10, 15]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  quoteDelete,
  quoteList,
  priview,
  quoteAdd,
  quoteEdit,
  exportQuote,
} from "@/api/request/dept";
import {
  PersonLogoList,
} from "@/api/banch/people.js";
import imgUpload from "@/components/myImgUpload/index.vue"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import storage from "@/utils/storage";
import { rowspan } from "@/utils/rowSpan";
export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return {
      TotalPriceList: [],
     
      ruleForm:{
        logoId:"",
        id:"",
        type:""
      },
      list: [],
      logoIdList:[],
      options: [
        {
          value: 1,
          label: "展厅预算",
        },
        {
          value: 2,
          label: "大屏预算",
        },
        {
          value: 3,
          label: "弱点项目预算",
        },
      ],
      priviewList: [],
      addEditform: {
        id: "",
        type: "",
        quoteTitle: "",
        remark: "",
      },
      quotoTitle: "",
      addEditformRules: {
        quoteTitle: [
          { required: true, message: "请输入报价名称", trigger: "blur" },
        ],
      },
      totalPrice: 0,
      total: 0,
      page: {
        pageNum: 1,
        pageSize: 5,
        quoteTitle: "",
      },
      dialogVisibleOne: false,
      dialogVisibleTwo: false,
      downDialogVisible:false,
      tableData: [],
      tableDataTwo: [],
      quoteTitle: "",
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "创建报价单",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
     
      dialogFormVisible: false,

      formLabelWidth: "120px",
      rules:{
        logoId:[{ required: true, message: '请输入公司名称', trigger: 'blur' },]
      }
    };
  },
  created() {
    this.getList(this.page);
  },
  mounted() {
    this.getLogoIdList()
  },
  methods: {
    changeType(val) {
     
    },
    getTime() {
      let a = "2021-07-30T14:05:45.000+0800";
      let b = a.splice(0, 10);
     
    },
    getKeys() {
      return this.$uuid.v1();
    },
    getLogoIdList(){
      PersonLogoList().then(res=>{
      
        this.logoIdList=res.rows
      },err=>{
      
      })
    },
    // logoId
     downEnter(){
      
         exportQuote(this.ruleForm).then(
        (res) => {
          this.download(res.msg);
          this.downDialogVisible=false
        },
        (err) => {
         
        }
      );
     },
    //导出
    expor(row, type) {
      
      this.downDialogVisible=true
      this.ruleForm.logoId="",
      this.ruleForm.id=row.id
      this.ruleForm.type=type
    },
    // 新增修改报价单
    addEditBtn() {
      if (this.title === "创建报价单") {
      
        this.$refs.addEditform.validate((valid) => {
          if (valid) {
            let { quoteTitle, remark, type } = this.addEditform;
            quoteAdd({ quoteTitle, remark, type }).then(
              (res) => {
                this.$message({
                  message: "恭喜你,创建成功",
                  type: "success",
                });
                this.dialogVisibleOne = false;
                this.getList();
                let mess = res.data;
                mess.quotoTitle = res.data.quoteTitle;
                storage.quoteId = mess;
                this.$router.push({
                  path: "/region",
                });
              },
              (err) => {
              
                this.$message({
                  message: "很遗憾，创建失败",
                  type: "success",
                });
              }
            );
          } else {
         
            return false;
          }
        });
      } else {
        let { quoteTitle, remark, id } = this.addEditform;
      
        this.$refs.addEditform.validate((val) => {
          if (val) {
            quoteEdit({ quoteTitle, remark, id }).then(
              (res) => {
              
                this.$message({
                  message: "恭喜你，修改成功",
                  type: "success",
                });
                this.dialogVisibleOne = false;
                this.getList();
              },
              (err) => {
              
                this.$message({
                  message: "很遗憾，修改失败",
                  type: "error",
                });
              }
            );
          } else {
            return false;
          }
        });
      }
    },
    // 取消预览弹窗
    goHome() {
      this.dialogVisibleTwo = false;
    },
    //预览详情
    hadprive(row) {
    
      this.dialogVisibleTwo = true;
      priview({ id: row.id }).then(
        (res) => {
        
          this.list = res.data.areaList;
          this.TotalPriceList = res.data;
          let a = res.data.areaList;
          let newa = [];
          a.forEach((item) => {
            if (item.displayList) {
              item.displayList.forEach((_item) => {
                let newobj = { areaName: item.areaName, ..._item };
                newa.push(newobj);
              });
            } else {
              newa.push({ areaName: item.areaName });
            }
          });
          this.tableDataTwo = newa;
        

          // this.quotoTitle=res.data.quotoTitle
          // //计算总价
          let arr = [];
          res.data.areaList.forEach((item) => {
            let areaName = item.areaName;
            item.displayList.forEach((item1) => {
              let displayName = item1.displayName;
              item1.productList.forEach((item2) => {
                let obj = { ...item2, areaName, displayName };
                arr.push(obj);
              });
            });
          });
          let bb = rowspan(arr);
         
          this.list = bb;
          let totalPrice = 0;
          // let TotalCost=0
          arr.forEach((item) => {
           
            totalPrice += item.priceCount;
            // this.tableData.forEach(_item=>{
            //   if(_item.id==item.id){
            //     _item[totalPrice]
            //   }
            // })
          });
          this.totalPrice = totalPrice.toFixed(2);
        },
        (err) => {
         
        }
      );
    },

    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getList(this.page);
    },
    // 页数
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getList(this.page);
      // this.getList();
    },
    /** 查询报价清单列表 */
    getList(val) {
      this.loading = true;
      quoteList(val)
        .then((res) => {
        
          this.loading = false;
          this.total = res.total;
          this.tableData = res.rows;
        })
        .catch((e) => {
         
        });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置

    /** 搜索按钮操作 */
    handleQuery() {
    
      this.getList(this.page);
    },
    /** 重置按钮操作 */

    /** 新增按钮操作 */
    getAdd() {
      this.title = "创建报价单";
      this.dialogVisibleOne = true;
      this.addEditform.id = "";
      this.addEditform.quoteTitle = "";
      this.addEditform.remark = "";
    },

    /** 编辑按钮操作 */
    editBtn(row) {
     
      this.title = "编辑报价单";
      // this.dialogVisibleOne= true;
      // this.addEditform.id=row.id;
      // this.addEditform.quoteTitle=row.quotoTitle
      // this.addEditform.remark=row.remark;
      storage.quoteId = row;
      this.$router.push({
        path: "/region",
        // query:{
        //   id:row.id,
        //   quotoTitle:row.quotoTitle
        // }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除名称为"' + row.quotoTitle + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          quoteDelete(row.id).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 30px;
}
.el-table-column {
  display: flex;
  justify-content: space-between;
}
.footer {
  width: 100%;
  height: 100px;
  display: flex;
  align-items: center;
  display: flex;
  justify-content: flex-end;
}
.title {
  line-height: 50px;
  font-size: 24px;
  font-weight: bold;
}
table {
  border-collapse: collapse;
  text-align: center;
  font-size: 12px;
}
td,
th {
  width: 150px;
  height: 30px;
  border-collapse: collapse;
  border: 1px solid grey;
}
.overCell {
  width: 250px;
  height: 70px !important;
  padding: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: wrap;
}
</style>